package com.kgc.personprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.personcommon.pojo.Department;
import com.kgc.personcommon.pojo.Permission;
import com.kgc.personcommon.pojo.Role;
import com.kgc.personprovider.dao.RoleMapper;
import com.kgc.personprovider.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public ResponseMsg getByPage(Map<String, Object> params) {
        logger.info("RoleServiceImpl getByPage, params={}", params);
        ResponseMsg msg = new ResponseMsg();

        int pageNow  = Integer.parseInt(params.get("pageNow").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        PageHelper.startPage(pageNow, pageSize);

        List<Role> list = roleMapper.getAll(params);
        logger.debug("roleMapper getByPage returned list.size={}", list.size());

        PageHelper.clearPage();
        int total = roleMapper.getCount(params);
        logger.debug("roleMapper getByPage total={}", total);

        PageInfo<Role> pi = new PageInfo<>();
        if (!list.isEmpty()) {
            pi.setList(list);
            pi.setPageNo(pageNow);
            pi.setPageSize(pageSize);
            pi.setTotal(total);
            msg.setCode("200");
            msg.setData(pi);
        } else {
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg add(Role role) {
        logger.info("RoleServiceImpl add, role={}", role);
        ResponseMsg msg = new ResponseMsg();
        Date now = new Date();
//        role.setCreateUser("admin");
        role.setCreateDate(now);
//        role.setUpdateUser("admin");
        role.setUpdateDate(now);
        role.setIs_delete(0);
        int c = roleMapper.addRole(role);
        logger.debug("roleMapper addRole count={} for roleId={}", c, role.getId());
        if (c == 0) {
            msg.setCode("201");
            return msg;
        }
        Map<String, Object> m = new HashMap<>();
        m.put("updateUser", role.getUpdateUser());
        m.put("updateDate", role.getUpdateDate());
        m.put("createUser", role.getCreateUser());
        m.put("createDate", role.getCreateDate());
        m.put("rid", role.getId());
        for (int did : role.getDids()) {
            m.put("did", did);
            int ct = roleMapper.addRoleDept(m);
            logger.debug("roleMapper addRoleDept count={} for {}", ct, m);
        }
        for (int pid : role.getPids()) {
            m.put("pid", pid);
            int ct = roleMapper.addRolePerm(m);
            logger.debug("roleMapper addRolePerm count={} for {}", ct, m);
        }
        msg.setCode("200");
        return msg;
    }

    @Override
    public ResponseMsg update(Role role) {
        logger.info("RoleServiceImpl update, role={}", role);
        ResponseMsg msg = new ResponseMsg();
        Date now = new Date();
//        role.setUpdateUser("11111");
        role.setUpdateDate(new Date());

        int c = roleMapper.updateRole(role);
        logger.debug("roleMapper updateRole count={} for roleId={}", c, role.getId());
        if (c == 0) {
            msg.setCode("201");
            return msg;
        }
        Role roleTemp = roleMapper.getRoleById(role.getId()+"");
        Map<String, Object> m = new HashMap<>();
        m.put("rid", role.getId());
        m.put("updateUser", role.getUpdateUser());
        m.put("updateDate", role.getUpdateDate());
        m.put("createUser", roleTemp.getCreateUser());
        m.put("createDate", roleTemp.getCreateDate());
        int clearDept = roleMapper.delRoleDeptByRole(role.getId()+"");
        logger.debug("roleMapper delRoleDeptByRole count={} for roleId={}", clearDept, role.getId());
        for (int did : role.getDids()) {
            m.put("did", did);
            int ct = roleMapper.addRoleDept(m);
            logger.debug("roleMapper addRoleDept count={} for {}", ct, m);
        }

        // 权限更新
        int clearPerm = roleMapper.delRolePermByRole(role.getId()+"");
        logger.info("RoleServiceImpl delRolePermByRole count={} for roleId={}", clearPerm, role.getId());
        for (int pid : role.getPids()) {
            m.put("pid", pid);
            int ct = roleMapper.addRolePerm(m);
            logger.debug("RoleServiceImpl addRolePerm count={} for {}", ct, m);
        }
        msg.setCode("200");
        return msg;
    }

    @Override
    public ResponseMsg delete(String id) {
        logger.info("RoleServiceImpl delete, id={}", id);
        ResponseMsg msg = new ResponseMsg();

        int c = roleMapper.deleteRole(id);
        logger.debug("RoleServiceImpl deleteRole count={} for id={}", c, id);
        if (c > 0) {
            msg.setCode("200");
        } else {
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg getById(String id) {
        logger.info("RoleServiceImpl getById, id={}", id);
        ResponseMsg msg = new ResponseMsg();

        Role role = roleMapper.getRoleById(id);
        logger.debug("RoleServiceImpl getRoleById returned role={}", role);
        if (role != null) {
            msg.setCode("200");
            msg.setData(role);
        } else {
            msg.setCode("201");
        }
        return msg;
    }

    @Override
    public ResponseMsg getAll(Map<String, Object> params) {
        logger.info("RoleServiceImpl getAll, params={}", params);
        ResponseMsg msg = new ResponseMsg();

        List<Role> list = roleMapper.getAllRoles(params);
        logger.debug("RoleServiceImpl getAll returned count={}", list.size());
        if (!list.isEmpty()) {
            msg.setCode("200");
            msg.setData(list);
        } else {
            msg.setCode("201");
        }
        return msg;
    }
}
